<?php
//
//	file: includes/log/plug_advlog_profile.php
//	begin: 30/04/2007
//	$Author: christopher $
//	$Revision: 31 $
//	$Date: 2008-07-21 03:08:30 +0000 (Mon, 21 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: plug for advlog to viewprofile

if (!defined('IN_PHPBB'))
{
	die("Hacking attempt");
}

class advlog_profile
{
	// pulls ip tracks for user
	function view($view_user)
	{
		global $user, $config, $template;

		// check auth
		if (!$user->auth(POST_GROUPS_URL, AUTH_LOG_VIEWIP))
		{
			return;
		}

		$encoded_ip = $view_user->data['user_registered_ip'];
		$decoded_ip = ($encoded_ip) ? '' : decode_ip($encoded_ip);
		$hostname = ($view_user->data['user_registered_hostname'] == '') ? $user->lang('Not_recorded') : htmlspecialchars($view_user->data['user_registered_hostname']);

		// generic template stuff
		$template->set_switch('switch_log_viewip');
		$template->assign_vars(array(
			'L_USERNAME' => $user->lang('Username'),
			'L_LOGINS' => $user->lang('Logins'),
			'L_IP' => $user->lang('IP'),
			'L_BROWSER' => $user->lang('Browser'),
			'L_TIME' => $user->lang('Time'),
			'L_MODERATOR_IP_INFORMATION' => $user->lang('Moderator_ip_information'),
			'L_REGISTERED_IP_ADDRESS' => $user->lang('Registered_ip_address'),
			'L_REGISTERED_HOSTNAME' => $user->lang('Registered_hostname'),
			'L_OTHER_REGISTERED_IPS' => sprintf($user->lang('Other_registered_ips'), $decoded_ip),
			'L_OTHER_IPS' => $user->lang('Other_posted_ips'),
			'USER_EMAIL_ADDRESS' => $view_user->data['user_email'],
			'U_USER_IP_ADDRESS' => (!empty($decoded_ip)) ? sprintf($user->lang('logs_ip_whois'), $decoded_ip) : $user->lang('Not_recorded'),
			'USER_IP_ADDRESS' => $decoded_ip,
			'USER_REGISTERED_HOSTNAME' => $hostname,
			'L_NO_OTHER_REGISTERED_IPS' => $user->lang('No_other_registered_ips'),
			'L_NO_OTHER_REGISTERED_IPS' => $user->lang('No_other_registered_ips'),
			'L_NO_OTHER_POSTED_IPS' => $user->lang('No_other_posted_ips'),
			'L_NO_LOGINS' => $user->lang('No_logins'),
		));

		// users registered to IP
		if (!empty($encoded_ip))
		{
			$var = array('counter' => 'user_id', 'key' => 'user_registered_ip', 'value' => $encoded_ip, 'exclude_key' => 'user_id', 'exclude_value' => $view_user->data['user_id'], 'order' => 'user_regdate', 'limit' => $config->data['topics_per_page'], 'extra' => array('user_id', 'username', 'user_regdate', 'user_registered_hostname'));
			$key = 'u_start';

			$total = get_count_by_field(USERS_TABLE, $var, __FILE__, __LINE__);
			if ( $total > 0 )
			{
				$var['start'] = _read($key, TYPE_INT, 0);
				if($result = get_table_by_field(USERS_TABLE, $var, __FILE__, __LINE__))
				{
					while ( $row = $db->sql_fetchrow($result) )
					{
						$template->assign_block_vars('switch_log_viewip.user_ips', array(
							'USER_NAME' => $row['username'],
							'U_PROFILE' => $config->url('usercp', array('mode'=>'viewprofile',POST_USERS_URL=>$row['user_id'])),
							'USER_HOSTNAME' => htmlspecialchars($row['user_registered_hostname']),
							'TIME' => $user->date($row['user_regdate']),
						));
					}
				}
				$db->sql_freeresult($result);

				$pagination = new pagination($cp_requester, $parms);
				$pagination->display('pagination', $total, $var['limit'], $var['start'], true, $key);
				unset($pagination, $key, $var, $total);

			}
		}

		// ip addresses user posted from
		$var = array('counter' => 'post_id', 'key' => 'poster_id', 'value' => $view_user->data['user_id'], 'group' => 'poster_ip', 'order' => (SQL_LAYER == 'msaccess') ? 'COUNT(*)' : 'postings', 'limit' => $config->data['topics_per_page'], 'extra' => array('poster_ip', 'COUNT(*) AS postings'));
		$key = 'i_start';

		$total = get_count_by_field(POSTS_TABLE, $var, __FILE__, __LINE__);
		if ( $total > 0 )
		{
			$var['start'] = _read($key, TYPE_INT, 0);
			if($result = get_table_by_field(POSTS_TABLE, $var, __FILE__, __LINE__))
			{
				while ( $row = $db->sql_fetchrow($result) )
				{
					$ip = decode_ip($row['poster_ip']);

					$template->assign_block_vars('switch_log_viewip.posted_ips', array(
					'U_POSTER_IP' => sprintf($user->lang('logs_ip_url'), $ip),
					'POSTER_IP' => $ip,
					'POSTS' => $row['postings'] . ' ' . ($row['postings'] == 1) ? $user->lang('Post') : $user->lang('Posts'),
					'U_POSTS_LINK' => $config->url('search', array('mode'=>'results','search_author'=> urlencode($view_user->data['username']),'search_ip'=>$ip), true),
				));
				}
			}
			$db->sql_freeresult($result);

			$pagination = new pagination($cp_requester, $parms);
			$pagination->display('pagination', $total, $var['limit'], $var['start'], true, $key);
			unset($pagination, $key, $var, $total);
		}


		// logins from user
		$var = array('key' => 'login_userid', 'value' => $view_user->data['user_id'], 'order' => 'login_time', 'limit' => $config->data['topics_per_page'], 'extra' => array('*'));
		$key = 'l_start';

		$total = get_count_by_field(LOGINS_TABLE, $var, __FILE__, __LINE__);
		if ( $total > 0 )
		{
			$var['start'] = _read($key, TYPE_INT, 0);
			if($result = get_table_by_field(LOGINS_TABLE, $var, __FILE__, __LINE__))
			{
				while ( $row = $db->sql_fetchrow($result) )
				{
					$ip = decode_ip($row['login_ip']);

					$template->assign_block_vars('switch_log_viewip.logins', array(
					'U_IP' => sprintf($user->lang('logs_ip_url'), $ip),
					'IP' => $ip,
					'USER_AGENT' => htmlspecialchars( $row['login_user_agent'] ),
					'LOGIN_TIME' => $user->date($row['login_time']),
				));
				}
			}
			$db->sql_freeresult($result);

			$pagination = new pagination($cp_requester, $parms);
			$pagination->display('pagination', $total, $var['limit'], $var['start'], true, $key);
			unset($pagination, $key, $var, $total);
		}

		// cleanup
		unset($encoded_ip, $decoded_ip, $hostname);
		return;
	}
}

?>